package com.novunda.platform.security;

import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.pam.ModularRealmAuthenticator;
import org.apache.shiro.realm.Realm;

import java.util.Collection;

/**
 * 登陆认证策略选择器
 *
 * @author Lizy
 * @since 2016/9/21
 */
public class NovModularRealmAuthenticator extends ModularRealmAuthenticator {

    @Override
    protected AuthenticationInfo doAuthenticate(AuthenticationToken token) {
        assertRealmsConfigured();

        NovUsernamePasswordToken usernamePasswordToken = (NovUsernamePasswordToken) token;
        Collection<Realm> realms = getRealms();
        for (Realm realm : realms) {
            // 判断登陆类型并返回对应的检查规则
            if (realm.getName().equals(usernamePasswordToken.getLoginType().name())) {
                return doSingleRealmAuthentication(realm, token);
            }
        }
        return realms.size() == 1 ? doSingleRealmAuthentication(realms.iterator().next(), token) : doMultiRealmAuthentication(realms, token);

    }
}
